from django.db import models
from ckeditor.fields import RichTextField


from user.models import CustomUser






class Topic(models.Model):
    name = models.CharField(verbose_name="话题",max_length=100)
    description = RichTextField(verbose_name="话题含义")
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class Question(models.Model):
    title = models.CharField(verbose_name="标题", max_length=200)
    content = RichTextField(verbose_name="问题内容")
    author = models.ForeignKey(to=CustomUser, verbose_name="提问的用户",on_delete=models.CASCADE)
    topic = models.ForeignKey(to=Topic, verbose_name="所属话题",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title


class Answer(models.Model):
    content = RichTextField(verbose_name="回答内容")
    question = models.ForeignKey(to=Question, verbose_name="回答的问题", on_delete=models.CASCADE)
    author = models.ForeignKey(to=CustomUser, verbose_name="评论的用户",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.author.username